<template>
	<view class="login">
		<my-back></my-back>
		<u-gap height="120"></u-gap>
		<view class="title">微信登录</view>
		<view class="title">请绑定您的手机号</view>
		
		<u-gap height="120"></u-gap>
		<view class="form">
			<u-field v-model="form.mobile" :label-width="0" placeholder="手机号">
			</u-field>

		<!-- 	<u-field v-model="form.password" password :label-width="0" placeholder="密码">
			</u-field>

			<u-field v-model="form.password2" password :label-width="0" placeholder="确认密码">
			</u-field> -->

			<u-field v-model="form.code" :label-width="0" placeholder="验证码">
				<u-tag slot="right" mode="plain" :text="tips" type="success" @click="getCode"></u-tag>
			</u-field>
			<u-verification-code :seconds="60" ref="uCode" @change="codeChange"></u-verification-code>

			<!-- <u-field v-model="form.invite_code" :label-width="0" placeholder="邀请码(选填)">
			</u-field> -->
		</view>
		<u-gap height="120"></u-gap>

		<view class="btn" @click="bind">
			确认
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				tips: '获取验证码',
				form: {
					mobile: '',
					code: ''
				},
				unionid: ''
			};
		},
		onLoad(opt) {
			this.unionid = opt.unionid
		},
		methods: {
			codeChange(e) {
				this.tips = e
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$u.post('/code/mobile?type=bind', {
						mobile: this.form.mobile
					}).then(res => {
						uni.hideLoading();
						if (res.data.code == 0) {
							this.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						} else {
							this.$u.toast(res.data.msg)
						}
					})
				} else {
					this.$u.toast('稍后再试');
				}
			},
			bind() {
				if (this.form.mobile && this.form.code) {
					this.$u.post('/third/bind', {
						...this.form,
						unionid: this.unionid,
						type: 'wechat'
					}).then(res => {
						this.$msg(res.data.msg)
						if (res.data.code==0) {
							let {
								finance,
								userinfo,
								token
							} = res.data.result
							this.$store.commit('setFinance', finance)
							this.$store.commit('setToken', token);
							this.$store.commit('userLogin', userinfo);
							this.$goto('/pages/mine/index', 'tab');
						}
					})
				} else {
					this.$msg('请输入有效信息')
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}
</style>
